﻿Ext.namespace('EHSubmit.MessageBox.WriteMsg');

    //--------显示联系人信息----------
    EHSubmit.MessageBox.WriteMsg.showContact = function showContact(contact) {
        var RoleTypeEnum = {
            // 管理员
            Admin: 0,
            // 教师
            Teacher: 1,
            // 学生
            Student: 2
        }
        var showContact;
        
        //根据不同角色，做不同显示
        var role = contact.RoleId;
        switch (role) {
            case RoleTypeEnum.Admin:
                {
                    showContact = contact.Name.trim() + '(管理员)';
                    break;
                }
            case RoleTypeEnum.Teacher:
                {
                    showContact = contact.Name.trim() + '(师)<' + contact.Institute.trim() + ' ' + contact.Department.trim() + '>';
                    break;
                }
            case RoleTypeEnum.Student:
                {
                    showContact = contact.Name.trim() + '(生)<' + contact.Department.trim() + ' ' + contact.Class.trim() + '>';
                    break;
                }
        }
        var toTextfield = Ext.getCmp('to');
        toTextfield.setValue(showContact);
    };
    //------------发送消息-----------
    EHSubmit.MessageBox.WriteMsg.sendMsg = function sendMsg() {
        var frm = Ext.getCmp('subMsgForm');
        //------验证收件人(只会有正确的格式和为空两种情况)----------
        var toTextField = Ext.getCmp('to');
        var to = toTextField.value;    
        if (to) {
            if (frm.getForm().isValid()) { // 验证表单
                var msg = EHSubmit.MessageBox.msgData;
                var isInbox = EHSubmit.MessageBox.isInbox;
                var receiverID = null;
                var receiverName = null;
                if (isInbox) {
                    receiverID = msg.SenderID;
                    receiverName = msg.SenderName;
                }
                else {
                    receiverID = msg.ReceiverID;
                    receiverName = msg.ReceiverName;
                }
                var toTextfield = Ext.getCmp('to');
                var titleTextfield = Ext.getCmp('title');
                var content = Ext.getCmp('innerForm_down').getValue();
                
                //MID自增,SendTime.RecvTime服务端处理，IsViewed、IsSDeleted，IsSDeleted默认不处理
                var Message = {
//                    MID:'',
                    Title: titleTextfield.getValue(),
                    Content: content,
                    SenderID: CurrentUser.ID,      //当前用户
                    ReceiverID: receiverID,
                    SenderName: CurrentUser.Name,  //当前用户
                    ReceiverName: receiverName
//                    SendTime; '',
//                    RecvTime: '',
//                    IsViewed: 0,
//                    IsSDeleted: 0,
//                    IsRDeleted: 0
                }
                var msgJson = Ext.util.JSON.encode(Message);

                frm.getForm().submit({     // 提交
                    url: EHSubmit.ajaxRoot + 'MessageHandler.ashx?op=add',
                    waitTitle: '等待',
                    waitMsg: '正在发送，请稍后...',
                    method: 'POST',
                    params: {
                        msg: msgJson,
                    },
                    success: function(form, action) {
                        var successed = action.result.success;
                        if (successed) {
                            ///////////////  提交成功处理  ///////////////////
                            Ext.MessageBox.alert('提示', action.result.msg);
                        }
                        else {
                            Ext.MessageBox.alert('失败', '信息:' + action.result.msg);
                        }
                    },
                    failure: function(form, action) {
                        var errmsg = action.result.msg;
                        Ext.MessageBox.alert('错误', errmsg);
                    }   // failure  
                });
                //-----------关闭发件窗口------------
                var thisWin = Ext.getCmp('writeMsgWin');
                thisWin.close();
                //--------grid 需要刷新---------
                //Ext.getCmp('outboxGrid').store.reload();
            }  
        }
        else {
            toTextField.validateValue(to);
        }
    };


    //---------------写信息主窗口的上部----------
    EHSubmit.MessageBox.WriteMsg.innerForm_up = {
        id: 'innerForm_up',
        xtype: 'fieldset',
        flex: 0.25,
        //height:80,
        labelAlign: 'center',
        //anchor:'100%',
        defaults: { width: 300 },
        defaultType: 'textfield',
        items: [{
            id: 'to',
            xtype: 'trigger',
            fieldLabel: '收件人',
            name: 'to',
            disabled: true,
            allowBlank: false,
            blankText: '此处不能为空',
            emptyText: '选择一个联系人',
            onSelect: function(record) {

            },
            onTriggerClick: function() {
                if (this.menu == null) {
                    this.menu = new Ext.menu.Menu(EHSubmit.MessageBox.WriteMsg.Contact.selectMenu);
                }
                this.menu.show(this.el);  //显示该组件
            }
        }, {
            id: 'title',
            fieldLabel: '主题',
            //name: 'title',
            allowBlank: false,
            blankText: '此处不能为空'
}]
        };

    //----------------写信息主窗口的下部-------------
    EHSubmit.MessageBox.WriteMsg.innerForm_down = {
        xtype:'panel',
        layout:'fit',
        border:false,
        //子元素平均分配宽度 
        flex:0.9,
        items:{
            id:'innerForm_down',
            xtype: 'htmleditor',
            //name:'content',
            //emptyText: '这里填写作业说明',
            enableAlignments: false,
            enableLists: false
        }
    };

    //-------------写信息主窗口（暂时只有上下两部分）----------
    EHSubmit.MessageBox.WriteMsg.innerForm = {
        xtype: 'form',
        id: 'subMsgForm',
        layout: {
            type:'vbox',
            padding:'2',
            align:'stretch'
        },
        border:false,
        frame: true,
        monitorValid: true,
        //-------中间部分----------
        items: [EHSubmit.MessageBox.WriteMsg.innerForm_up, EHSubmit.MessageBox.WriteMsg.innerForm_down]

    };


    //-------包含写信息窗口的容器--------------
    EHSubmit.MessageBox.WriteMsg.window = {
        id: 'writeMsgWin',
        title: '写消息',
        //xtype:'window',
        width:700,
        height:450,
        plain:true,
        border: false,
        layout:'fit',
        modal:true,
        
        //-------topbar-------
        tbar: [{                     
            text: '发送',
            tooltip: '发送',
            iconCls: 'icon-send',
            handler: EHSubmit.MessageBox.WriteMsg.sendMsg
        }, '-', {
            text: '关闭',
            tooltip: '关闭',
            iconCls: 'close',
            handler: function() {
                //----关闭当前窗口---------
                var writeMsgWin = Ext.getCmp('writeMsgWin');
                writeMsgWin.close();
            }
        }],
        
        items: [EHSubmit.MessageBox.WriteMsg.innerForm],
        
            //-------footbar--------------
        fbar:[{
            text: '发送',
            tooltip: '发送',
            iconCls: 'icon-send',     //图标引用需写在css中
            handler: EHSubmit.MessageBox.WriteMsg.sendMsg
        }, '-', {
            text: '关闭',
            tooltip: '关闭',
            iconCls: 'close',
            handler: function() {
                //----关闭当前窗口---------
                var writeMsgWin = Ext.getCmp('writeMsgWin');
                writeMsgWin.close();
            }
        }],
        listeners:{
            activate:function(thisWin){
                var toTextfield = Ext.getCmp('to');
                var titleTextfield = Ext.getCmp('title');
                var replyToID = null;

                var msg = EHSubmit.MessageBox.msgData;
                if (msg.SenderID != null && msg.ReceiverID != null) {
                    var title = msg.Title;             //发件标题  msgWrite用
                    var senderID = msg.SenderID;
                    var receiverID = msg.ReceiverID;
                    var isInbox = EHSubmit.MessageBox.isInbox;
                    if (isInbox) {
                        //说明消息来自收件箱
                        replyToID = senderID;
                        title = 'Re: ' + title;
                    }
                    else {
                        //说明消息来自发件箱
                        replyToID = receiverID;
                    }
                    //-----根据传来的id，请求其相关数据------------
                    Ext.Ajax.request({
                        url: EHSubmit.ajaxRoot + 'MessageHandler.ashx',
                        success: function(response, opts) {

                            //获取服务器端的数据(解析后返回的是对象)
                            var contact = Ext.decode(response.responseText).contact;

                            EHSubmit.MessageBox.WriteMsg.showContact(contact);
                            titleTextfield.setValue(title);
                        },
                        //      failure: function(response, opts){
                        //          var result = Ext.decode(response.responseText).msg;
                        //          Ext.MessageBox.alert('错误',result);
                        //      },
                        params: { Op: 'sc', UID: replyToID }
                    });
                }
                else {   //直接点了grid上的写消息,则senderid和Receiverid为null
                    toTextfield.setValue('');
                    titleTextfield.reset();
                }
            }
        }
    };